﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class WebUserControl_CtrFilterADNCode : System.Web.UI.UserControl
{
    static string user = "";
    BCDataCenter.DataAccessLayer.DAL dt;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string error = "";
            try
            {
                dt = new BCDataCenter.DataAccessLayer.DAL();
                DataSet ds = new DataSet();
                ds = dt.MyExecuteReader(ref error, "GetUsers", CommandType.StoredProcedure);
                DropDownList1.DataSource = ds.Tables[0];
                DropDownList1.DataTextField = "UserDescription";
                DropDownList1.DataValueField = "UserName";
                DropDownList1.DataBind();
            }
            catch (Exception ex)
            {
                uscMsgBox1.AddMessage(ex.Message, uscMsgBox.enmMessageType.Error);
            }

        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string error = "";
        dt = new BCDataCenter.DataAccessLayer.DAL();
        int rownum = GridView1.Rows.Count;
        try
        {
            for (int i = 0; i < rownum; i++)
            {
                CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
                Label lb = (Label)GridView1.Rows[i].FindControl("Label2");
                string sqlstr = "delete FROM   UserAdnCodeFilter WHERE     (AdnCode = N'" + lb.Text.ToString() + "') AND (username = '" + user + "')";
                dt.MyExecuteNonQuerys(sqlstr, CommandType.Text, ref error);
                if (cb.Checked == true)
                {
                    sqlstr = "Insert into UserAdnCodeFilter (AdnCode, username)   values(N'" + lb.Text.ToString() + "','" + user + "')";
                    dt.MyExecuteNonQuerys(sqlstr, CommandType.Text, ref error);
                }
            }
            uscMsgBox1.AddMessage("Hoàn thành cập nhật thông tin", uscMsgBox.enmMessageType.Success);
        }
        catch (Exception ex)
        {
            uscMsgBox1.AddMessage(ex.Message, uscMsgBox.enmMessageType.Error);
        }
    }
    private void LoadTTPP()
    {        
        string error = "";
        DataSet ds = new DataSet();
        try
        {
            ds = dt.MyExecuteQuery(ref error, "GetADNCodeFilter", CommandType.StoredProcedure, new SqlParameter("@username", user));
            int row=ds.Tables[0].Rows.Count;
            if ( row> 0)
            {
                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();
                for (int i = 0; i < row; i++)
                {
                    CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
                    if (ds.Tables[0].Rows[i]["ischeck"].ToString() == "1")
                        cb.Checked = true;
                    else
                        cb.Checked = false;
                }
            }
            
        }
        catch (Exception ex)
        {
            uscMsgBox1.AddMessage(ex.Message, uscMsgBox.enmMessageType.Error);
        }

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        user = DropDownList1.SelectedValue.ToString();
        dt = new BCDataCenter.DataAccessLayer.DAL();
        LoadTTPP();
    }
}
